package cn.com.homeyoda.aimate.aihealth.ui.my.feedback;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.FEEDBACK_SUCCESS;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.FeedBackDetailsBean;
import cn.com.homeyoda.aimate.aihealth.bean.FeedBackListBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentMyChildBinding;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.problem.ProblemActivity;

public class FeedbackFragment extends BaseFragment<FragmentMyChildBinding, MyViewModel> {

    private FeedbackAdapter feedbackAdapter;

    @Override
    public MyViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MyViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getFeedbackList();
        binding.tvTitle.setText(getString(R.string.tab_wdfk));
        binding.ivEdit.setVisibility(View.VISIBLE);
        binding.ivEdit.setOnClickListener(v -> startActivity(new Intent(getActivity(), ProblemActivity.class)));
        initRecyclerView();
    }

    private void initRecyclerView() {
        feedbackAdapter = new FeedbackAdapter(R.layout.feedback_item);
        binding.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider_transparent));
        binding.rvMyContent.addItemDecoration(divider);
        binding.setAdapter(feedbackAdapter);
        feedbackAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            FeedBackListBean.RowsBean bean = (FeedBackListBean.RowsBean) adapter.getItem(position);
            Log.i("huage","RowsBean====="+bean);
            if (bean == null) return;
            Log.i("huage","RowsBean===id=="+bean.getId());
            viewModel.getFeedbackDetail(bean.getId());
        });

    }

    private void showFeedbackDialog(FeedBackDetailsBean.DataBean dataBean) {
        MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                .customView(R.layout.dialog_feedback, false)
                .show();
        TextView tvFeedbackTime = dialog.getCustomView().findViewById(R.id.tv_feedback_time);
        TextView tvQuestionType = dialog.getCustomView().findViewById(R.id.tv_question_type);
        TextView tvQuestionDesc = dialog.getCustomView().findViewById(R.id.tv_question_desc);
        TextView tvQuestionReply = dialog.getCustomView().findViewById(R.id.tv_question_reply);

        tvFeedbackTime.setText(dataBean.getAddTime());
        tvQuestionType.setText(dataBean.getType());
        tvQuestionDesc.setText(dataBean.getDescribe());
        if (dataBean.getHandleRemark() != null) {
            tvQuestionReply.setText("回复：" + dataBean.getHandleRemark());
        }else{
            tvQuestionReply.setText("感谢您的反馈，我们正在快马加鞭地处理!");
        }

        dialog.getView().findViewById(R.id.iv_close).setOnClickListener(v -> dialog.dismiss());
        if (dialog.getWindow() != null) {
            dialog.getWindow().setLayout(946, WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(0));
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.feedbackList.observe(this, list -> feedbackAdapter.setNewData(list));
        viewModel.feedbackDetail.observe(this, this::showFeedbackDialog);
        LiveEventBus.get(FEEDBACK_SUCCESS, boolean.class).observe(this, aBoolean -> {
            viewModel.getFeedbackList();
        });
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_child;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
